package com.doget.data.dialog;

import android.animation.Animator;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewAnimationUtils;
import android.view.ViewGroup;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.RequiresApi;

import com.doget.comment.utils.DisplayUtil;
import com.doget.data.MainActivity;
import com.doget.data.R;

import java.lang.ref.WeakReference;

/**
 * 预存缴费入口弹窗
 */
public class AdvPayHintDialog extends Dialog {
    private static int mCenterX;
    private static int mCenterY;
    private CustomConfirmInterface m_interface;
    private static MainActivity context;

    private WeakReference<Context> mContext = new WeakReference<>(null);
    private volatile static AdvPayHintDialog sDialog;
    private final LinearLayout container;
    private final TextView tvSure;

    private AdvPayHintDialog(Context context, String titleMsg, String buttonMsg) {
        super(context, R.style.CustomProgressDialog);

        mContext = new WeakReference<>(context);

        @SuppressLint("InflateParams")
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_adv_pay_hint, null);

        container = view.findViewById(R.id.container);
        tvSure = view.findViewById(R.id.tv_sure);
        TextView tvMsg = view.findViewById(R.id.tv_message);
        if (titleMsg != null) {
            tvMsg.setText(titleMsg);
        }
        if (buttonMsg != null) {
            tvSure.setText(buttonMsg);
        }
        tvSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                showAnimation();

            }
        });


        ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        lp.width = (int) (DisplayUtil.getScreenWidth(context)  );
        addContentView(view, lp);

    }

    private void showAnimation() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            container.post(new Runnable() {
                @Override
                public void run() {
                    createFinishAnimator(mCenterX,mCenterY);
//                    Animator animator = createFinishAnimator(mCenterX, mCenterY);
//                    animator.start();
                }
            });
        } else {
            dismiss();
        }
    }


    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    private Animator createFinishAnimator(int x, int y) {
//        参数2：centerX动画中心x轴坐标
//        参数3：centerY动画中心y轴坐标
//        参数4：startRadius动画开始的半径
//        参数5：endRadius动画结束的半径
        int measuredWidth = container.getMeasuredWidth();
        int measuredHeight = container.getMeasuredHeight();
        float startRadius = (float) Math.hypot(container.getHeight(), container.getWidth());
        float endRadius = 0;
        Animator animator = ViewAnimationUtils.createCircularReveal(
                container, x, y,
                startRadius,
                endRadius);
        animator.setDuration(660);
        animator.setInterpolator(new AccelerateDecelerateInterpolator());
        //判断标志位reversed，true则为添加返回键版动画监听器，false则为跳转动画开启版
        // if (!reversed)
        animator.addListener(animatorListener1);
        return animator;
    }



    private Animator.AnimatorListener animatorListener1 = new Animator.AnimatorListener() {
        @Override
        public void onAnimationStart(Animator animation) {
//            content.setVisibility(View.VISIBLE);//跳转进来时，（因为finish之前会将之设置为不可见，）
            // 根布局要设置为可见，与finish部分的不可见相对应
//            mPuppet.setAlpha(1);
        }

        @Override
        public void onAnimationEnd(Animator animation) {
//            container.startAnimation(createAlphaAnimation());
//            container.setVisibility(View.INVISIBLE);//动画结束时，揭露动画设置为不可见
            container.setVisibility(View.INVISIBLE);
            dismiss();

        }

        @Override
        public void onAnimationCancel(Animator animation) {
        }

        @Override
        public void onAnimationRepeat(Animator animation) {
        }
    };


    public static synchronized void showLoading(MainActivity activity, CustomConfirmInterface custInterface, boolean cancelable, String titleMsg, String buttonMsg, int x, int y) {
        mCenterX = x;
        mCenterY = y;

        context = activity;
        if (sDialog != null && sDialog.isShowing()) {
            sDialog.dismiss();
        }

        if (context == null) {
            return;
        }
        sDialog = new AdvPayHintDialog(context, titleMsg, buttonMsg);
        sDialog.setCancelable(cancelable);
        sDialog.m_interface = custInterface;


        if (sDialog != null && !sDialog.isShowing()) {
            if (context instanceof Activity && !((Activity) context).isFinishing()) {
                sDialog.show();
            } else {
                sDialog.show();
            }
        }
    }

    public static synchronized void stopLoading() {
        if (sDialog != null && sDialog.isShowing()) {
            sDialog.dismiss();
        }
        sDialog = null;
    }

    public interface CustomConfirmInterface {
        void confirmSureButtonClick();
    }


}
